package com.link.base.tencent.corpwx.message.service;

import com.link.base.tencent.corpwx.agent.service.AgentService;
import com.link.base.tencent.corpwx.message.model.MessageBase;
import com.link.base.tencent.corpwx.message.model.MessageMiniProgram;
import com.link.base.tencent.corpwx.message.model.MessageMpNews;
import com.link.base.tencent.corpwx.message.model.MessageNews;
import com.link.base.tencent.corpwx.message.util.AgentMessageUtil;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用消息服务
 *
 * @author 欧阳振强
 * @date 2020/01/03
 */
@Service
public class AgentMessageServiceImpl extends BasicServiceImpl<MessageBase> implements AgentMessageService {

    @Resource
    private AgentService agentService;

    @Override
    public void sendAgentTextMsg(MessageBase messageBase, String content) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentTextMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, content);
    }

    @Override
    public void sendAgentFileMsg(MessageBase messageBase, String mediaId) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentFileMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, mediaId);
    }

    @Override
    public void sendAgentImageMsg(MessageBase messageBase, String mediaId) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentImageMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, mediaId);
    }

    @Override
    public void sendAgentVoiceMsg(MessageBase messageBase, String mediaId) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentVoiceMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, mediaId);
    }

    @Override
    public void sendAgentVideoMsg(MessageBase messageBase, String mediaId, String title, String description) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentVideoMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, mediaId, title, description);
    }

    @Override
    public void sendAgentTextCardMsg(MessageBase messageBase, String title, String description, String msgUrl, String btnTxt) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentTextCardMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, title, description, msgUrl, btnTxt);
    }

    @Override
    public void sendAgentNewsMsg(MessageBase messageBase, List<MessageNews> messageNewsList) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentNewsMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, messageNewsList);
    }

    @Override
    public void sendAgentMpNewsMsg(MessageBase messageBase, List<MessageMpNews> messageMpNewsList) throws Exception {
        String agentId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(agentId);
        AgentMessageUtil.sendAgentMpNewsMsg(accessToken, messageBase.isSendAll(), messageBase.getUserIdList(), messageBase.getPartyIdList(),
            messageBase.getTagIdList(), agentId, messageMpNewsList);
    }

    @Override
    public void sendAgentMpNewsMsg(MessageBase messageBase, MessageMiniProgram messageMiniProgram) throws Exception {
        // 小程序Id
        String appId = messageBase.getAgentId();
        String accessToken = agentService.getExistAccessToken(appId);
        AgentMessageUtil.sendMiniProgramMsg(accessToken, messageBase.getUserIdList(), messageBase.getPartyIdList(), messageBase.getTagIdList(),
                appId, messageMiniProgram.getPage(), messageMiniProgram.getTitle(), messageMiniProgram.getDescription(),
                messageMiniProgram.getEmphasisFirstItem(), messageMiniProgram.getContentItems());
    }
}
